﻿<%@ WebHandler Language="C#" Class="storagefilepichandler" %>

using System;
using System.Web;
using System.IO;
using WuDada.Provider.ResourceHandle;
using WuDada.Provider.ResourceHandle.Service;
using WuDada.Provider.ResourceHandle.Domain;
using WuDada.Core.Generic.Util;
using System.Drawing.Imaging;

public class storagefilepichandler : IHttpHandler
{
    public void ProcessRequest (HttpContext context) {

        StorageFactory storageFactory = new StorageFactory();
        IStorageFileService storageFileService = storageFactory.GetStorageFileService();

        int fileId = QueryStringHelper.GetInteger("fileid", 0);
        int width = QueryStringHelper.GetInteger("width", 0);

        StorageFileVO storageFileVO = storageFileService.GetStorageFileById(fileId);
        if (storageFileVO != null)
        {
            FileInfo fileInfo = new FileInfo(storageFileVO.CurrentPath);

            if (fileInfo.Exists)
            {
                if (width > 0)
                {
                    DirectoryInfo directoryInfo = fileInfo.Directory;
                    //建立縮圖
                    string resizePicPath = string.Format("width{0}_{1}", width, fileInfo.Name);

                    string fullPath = Path.Combine(directoryInfo.FullName, resizePicPath);
                    if (!File.Exists(fullPath))
                    {
                        ImageUtil.ToSmallPicInWidth(fileInfo.FullName, fullPath, width, ImageFormat.Jpeg);
                    }

                    FileInfo newFileInfo = new FileInfo(fullPath);
                    if (newFileInfo.Exists)
                    {
                        fileInfo = new FileInfo(fullPath);
                    }
                }


                context.Response.Clear();
                using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open))
                {
                    int buffersize = int.Parse(fs.Length.ToString());
                    byte[] buffer = new byte[buffersize];
                    int count = fs.Read(buffer, 0, buffersize);

                    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.UTF8));
                    context.Response.ContentType = "image/jpeg";

                    context.Response.OutputStream.Write(buffer, 0, buffersize);
                    context.Response.Flush();
                    fs.Dispose();
                }
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}